Changing comments layout preparing for generated documentation with Phpdocumentor
[lhc/web/wiklou.git] / includes / SpecialBlockip.php
1 <?php
2 /**
3 * Constructor for Special:Blockip page
4 *
5 */
6
7 /**
8 * Constructor
9 */
10 function wfSpecialBlockip() {
11 global $wgUser, $wgOut, $wgRequest;
12
13 if ( ! $wgUser->isSysop() ) {
14 $wgOut->sysopRequired();
15 return;
16 }
17 $ipb = new IPBlockForm();
18
19 $action = $wgRequest->getVal( 'action' );
20 if ( "success" == $action ) { $ipb->showSuccess(); }
21 else if ( $wgRequest->wasPosted() && "submit" == $action ) { $ipb->doSubmit(); }
22 else { $ipb->showForm( "" ); }
23 }
24
25 /**
26 * Form object
27 *
28 */
29 class IPBlockForm {
30 var $BlockAddress, $BlockExpiry, $BlockReason;
31
32 function IPBlockForm() {
33 global $wgRequest;
34 $this->BlockAddress = $wgRequest->getVal( 'wpBlockAddress', $wgRequest->getVal( 'ip' ) );
35 $this->BlockReason = $wgRequest->getText( 'wpBlockReason' );
36 $this->BlockExpiry = $wgRequest->getVal( 'wpBlockExpiry' );
37 }
38
39 function showForm( $err )
40 {
41 global $wgOut, $wgUser, $wgLang, $wgDefaultBlockExpiry;
42 global $wgRequest;
43
44 $wgOut->setPagetitle( htmlspecialchars( wfMsg( "blockip" ) ) );
45 $wgOut->addWikiText( htmlspecialchars( wfMsg( "blockiptext" ) ) );
46
47 if ( is_null( $this->BlockExpiry ) || $this->BlockExpiry === "" ) {
48 $this->BlockExpiry = $wgDefaultBlockExpiry;
49 }
50
51 $mIpaddress = htmlspecialchars( wfMsg( "ipaddress" ) );
52 $mIpbexpiry = htmlspecialchars( wfMsg( "ipbexpiry" ) );
53 $mIpbreason = htmlspecialchars( wfMsg( "ipbreason" ) );
54 $mIpbsubmit = htmlspecialchars( wfMsg( "ipbsubmit" ) );
55 $titleObj = Title::makeTitle( NS_SPECIAL, "Blockip" );
56 $action = $titleObj->escapeLocalURL( "action=submit" );
57
58 if ( "" != $err ) {
59 $wgOut->setSubtitle( htmlspecialchars( wfMsg( "formerror" ) ) );
60 $wgOut->addHTML( "<p class='error'>{$err}</p>\n" );
61 }
62
63 $scBlockAddress = htmlspecialchars( $this->BlockAddress );
64 $scBlockExpiry = htmlspecialchars( $this->BlockExpiry );
65 $scBlockReason = htmlspecialchars( $this->BlockReason );
66
67 $wgOut->addHTML( "
68 <form id=\"blockip\" method=\"post\" action=\"{$action}\">
69 <table border='0'>
70 <tr>
71 <td align=\"right\">{$mIpaddress}:</td>
72 <td align=\"left\">
73 <input tabindex='1' type='text' size='20' name=\"wpBlockAddress\" value=\"{$scBlockAddress}\" />
74 </td>
75 </tr>
76 <tr>
77 <td align=\"right\">{$mIpbexpiry}:</td>
78 <td align=\"left\">
79 <input tabindex='2' type='text' size='20' name=\"wpBlockExpiry\" value=\"{$scBlockExpiry}\" />
80 </td>
81 </tr>
82 <tr>
83 <td align=\"right\">{$mIpbreason}:</td>
84 <td align=\"left\">
85 <input tabindex='3' type='text' size='40' name=\"wpBlockReason\" value=\"{$scBlockReason}\" />
86 </td>
87 </tr>
88 <tr>
89 <td>&nbsp;</td>
90 <td align=\"left\">
91 <input tabindex='4' type='submit' name=\"wpBlock\" value=\"{$mIpbsubmit}\" />
92 </td>
93 </tr>
94 </table>
95 </form>\n" );
96
97 }
98
99 function doSubmit() {
100 global $wgOut, $wgUser, $wgLang;
101 global $wgSysopUserBans, $wgSysopRangeBans;
102
103 $userId = 0;
104 $this->BlockAddress = trim( $this->BlockAddress );
105 $rxIP = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
106
107 # Check for invalid specifications
108 if ( ! preg_match( "/^$rxIP$/", $this->BlockAddress ) ) {
109 if ( preg_match( "/^($rxIP)\\/(\\d{1,2})$/", $this->BlockAddress, $matches ) ) {
110 if ( $wgSysopRangeBans ) {
111 if ( $matches[2] > 31 || $matches[2] < 16 ) {
112 $this->showForm( wfMsg( "ip_range_invalid" ) );
113 return;
114 }
115 $this->BlockAddress = Block::normaliseRange( $this->BlockAddress );
116 } else {
117 # Range block illegal
118 $this->showForm( wfMsg( "range_block_disabled" ) );
119 return;
120 }
121 } else {
122 # Username block
123 if ( $wgSysopUserBans ) {
124 $userId = User::idFromName( $this->BlockAddress );
125 if ( $userId == 0 ) {
126 $this->showForm( wfMsg( "nosuchuser", htmlspecialchars( $this->BlockAddress ) ) );
127 return;
128 }
129 } else {
130 $this->showForm( wfMsg( "badipaddress" ) );
131 return;
132 }
133 }
134 }
135
136 if ( $this->BlockExpiry == "infinite" || $this->BlockExpiry == "indefinite" ) {
137 $expiry = '';
138 } else {
139 # Convert GNU-style date, returns -1 on error
140 $expiry = strtotime( $this->BlockExpiry );
141
142 if ( $expiry < 0 ) {
143 $this->showForm( wfMsg( "ipb_expiry_invalid" ) );
144 return;
145 }
146
147 $expiry = wfUnix2Timestamp( $expiry );
148
149 }
150
151
152 if ( "" == $this->BlockReason ) {
153 $this->showForm( wfMsg( "noblockreason" ) );
154 return;
155 }
156
157 # Create block
158 # Note: for a user block, ipb_address is only for display purposes
159
160 $ban = new Block( $this->BlockAddress, $userId, $wgUser->getID(),
161 wfStrencode( $this->BlockReason ), wfTimestampNow(), 0, $expiry );
162 $ban->insert();
163
164 # Make log entry
165 $log = new LogPage( 'block' );
166 $log->addEntry( 'block', Title::makeTitle( NS_USER, $this->BlockAddress ), $this->BlockReason );
167
168 # Report to the user
169 $titleObj = Title::makeTitle( NS_SPECIAL, "Blockip" );
170 $wgOut->redirect( $titleObj->getFullURL( "action=success&ip={$this->BlockAddress}" ) );
171 }
172
173 function showSuccess() {
174 global $wgOut, $wgUser;
175
176 $wgOut->setPagetitle( wfMsg( "blockip" ) );
177 $wgOut->setSubtitle( wfMsg( "blockipsuccesssub" ) );
178 $text = wfMsg( "blockipsuccesstext", $this->BlockAddress );
179 $wgOut->addWikiText( $text );
180 }
181 }
182
183 ?>